<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class ClcwChipLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $clcwChipModel;

    public function _initialize()
    {
        $this->clcwChipModel = D('ClcwChip', 'Model');
    }

    /**
     * 获取碎片列表
     *
     * @param int   $page
     * @param int   $size
     * @param array $params
     */
    public function getClcwChipList($page, $size, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        $map = [];
        if ($params['desc']) {
            $map['desc'] = ['like', "%" . $params['desc'] . "%"];
        }
        if ($params['name']) {
            $map['name'] = ['like', "%" . $params['name'] . "%"];
        }
        $cols = 'id,name,content,desc,update_time,create_time';
        $order = 'id DESC';
        $total = $this->clcwChipModel->where($map)->count();
        $list = $this->clcwChipModel->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order($order)
            ->select();
        // echo M()->getlastsql();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
                $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }

    /**
     * 获取碎片信息
     *
     * @param int $id
     *
     * @return array
     */
    public function getClcwChipInfoById($id = 0)
    {
        if (empty($id)) {
            return false;
        }
        return $this->clcwChipModel->find($id);
    }

    /**
     * 获取碎片信息
     *
     * @param string $name
     *
     * @return array
     */
    public function getClcwChipInfoByName($name = '')
    {
        if (empty($name)) {
            return false;
        }
        $condition = [];
        $condition['name'] = ['eq', $name];
        return $this->clcwChipModel->where($condition)->find();
    }

    /**
     * @param array $data
     *
     * @return mixed
     */
    public function addClcwChip($data = [])
    {
        $time = NOW_TIME;
        $data['update_time'] = $time;
        $data['create_time'] = $time;
        return $this->clcwChipModel->add($data);
    }

    /**
     * 更新碎片信息
     *
     * @param array $data
     *
     * @return int
     */
    public function updateClcwChip($data = [])
    {
        if (empty($data) || empty($data['id'])) {
            return false;
        }
        $data['update_time'] = NOW_TIME;
        return $this->clcwChipModel->save($data);
    }
}